$(document).ready(function() {
    // 点击页码跳转
    /*var goThisPage = function() {
        var a = $(this).val();
        alert(a);
    }*/

    changState = function(obj) {
        var btnText = $(obj).html();
        var userId = $(obj).parent().attr("id");
        var urlUserDisable = '/manage/user/disable.do';
        var urlUserEnable = '/manage/user/enable.do';
        var url = '';
        if(btnText == "禁用") {
            url = urlUserDisable;
        } else {
            url = urlUserEnable;
        }
        $.ajax({
            type: "post",
            url: url,
            data: {
                userId : userId
            },
            cache: false,
            async : true,
            dataType: "json",
            success: function (data)
            {
                alert('修改成功!');
                if(btnText == "禁用") {
                    $(obj).html("启用");
                    $(obj).removeClass("btn btn-warning");
                    $(obj).addClass("btn btn-success");
                } else {
                    $(obj).html("禁用");
                    $(obj).removeClass("btn btn-success");
                    $(obj).addClass("btn btn-warning");
                }
                ajaxPage($("#pageNum").val());
            },
            error:function () {
                alert("请求失败！");
            }
        });
    }

    selectUser = function(obj) {
        var userId = $(obj).parent().attr("id");

        $.ajax({
            type: "post",
            url: '/manage/user/select_user.do',
            data: {
                userId : userId
            },
            cache: false,
            async : true,
            dataType: "json",
            success: function (data)
            {
                $("#updateUsername").val(data.data.username);
                $("#upadtePhoneNumber").val(data.data.phoneNumber);
                if(data.data.role == '1') {
                    $("#updateRole").val("管理员");
                } else if(data.data.role == '2') {
                    $("#updateRole").val("B1运营专员");
                } else if(data.data.role == '3') {
                    $("#updateRole").val("B2运营专员");
                } else {
                    $("#updateRole").val("未设置类型");
                }
                $("#updateId").val(data.data.id);
            },
            error:function () {
                alert("请求失败！");
            }
        });
    }

    updateInformation = function () {
        var username = $("#updateUsername").val();
        var phonNumber = $("#upadtePhoneNumber").val();
        var role = $("#updateRole option:selected").val();

        if( role == '全部') {
            role = 'all';
        } else if(role == '管理员'){
            role = '1';
        } else if(role == 'B1运营专员') {
            role = '2';
        } else if(role == 'B2运营专员') {
            role = '3';
        }
        var userId = $("#updateId").val();
        $.ajax({
            type: "post",
            url: '/manage/user/update.do',
            data: {
                username : username,
                phonNumber : phonNumber,
                role : role,
                userId : userId
            },
            cache: false,
            async : true,
            dataType: "json",
            success: function (data)
            {
                ajaxPage($("#pageNum").val());
                alert('更新成功');
            },
            error:function () {
                alert("请求失败！");
            }
        });
    }

    register = function() {
        var username = $("#reUsername").val();
        var phonNumber = $("#rePhoneNumber").val();
        var domainAccount = $("#reDomainAccount").val();
        var userstate = $("#reState").val();
        var role = $("#reRole option:selected").val();

        if( role == '全部') {
            role = 'all';
        } else if(role == '管理员'){
            role = '1';
        } else if(role == 'B1运营专员') {
            role = '2';
        } else if(role == 'B2运营专员') {
            role = '3';
        }

        if( userstate == '全部') {
            userstate = 'all';
        } else if(userstate == '已启用'){
            userstate = '1';
        } else if(userstate == '禁用') {
            userstate = '0';
        }

        $.ajax({
            type: "post",
            url: '/manage/user/insert.do',
            data: {
                username : username,
                phonNumber : phonNumber,
                role : role,
                state : userstate,
                phonNumber : phonNumber,
                domainAccount : domainAccount
            },
            cache: false,
            async : true,
            dataType: "json",
            success: function (data)
            {
                ajaxPage($("#pages").val());
                alert("注册成功");
            },
            error:function () {
                alert("请求失败！");
            }
        });

    }

    /*$("#ul li").click(function() {
        alert('a');
    });

    $("#ul a").click(function() {
        alert('q');
    });*/

    // 分页导航显示
    var paging = function(currentPage, totalPage) {
        if(currentPage == null && totalPage == null) {
            currentPage = $("#pageNum").val();
            totalPage = $("#pages").val();
        }
        var lis = "";

        $("#ul li:not(:first):not(:last)").remove();

        if(3 <= currentPage) {
            lis += '<li ><a class="go-page">'+ (currentPage - 2) + '</a></li>';
        }
        if( 2 <= currentPage) {
            lis += '<li ><a class="go-page">' + (currentPage - 1) + '</a></li>';
        }
        lis += '<li class="active"><a aria-label="Previous" class="go-page" id="go">' + (currentPage) + '</a></li>';
        if(totalPage - currentPage >=1) {
            lis += '<li ><a>'+ (currentPage -1 + 2) + '</a></li>';
        }
        if(totalPage - currentPage >= 2) {
            lis += '<li ><a>' + (currentPage -1 + 3) + '</a></li>';
        }

        $("#ul li").eq(0).after(lis);

        $("#pages").val(totalPage);
        $("#pageNum").val(currentPage);
    }

    // 初始化页面导航
    paging(null, null);

    // 点击事件进行查询
    $("#search").click(function() {
        ajaxPage(1);
    });

    // enter键执行查询
    /*document.onkeydown = function (event){
        ajaxPage(1);
    };*/

    // 点击下一页触发
    $("#next_page").click(function() {
        if($("#pages").val() > $("#pageNum").val()){
            var currentPage = $("#pageNum").val() -1 + 2;
            $("#pageNum").val(currentPage);
            ajaxPage(currentPage);
        }
    });

    // 点击上一页触发
    $("#up_page").click(function() {
        if($("#pageNum").val() >= 2) {
            var currentPage = $("#pageNum").val() -1 ;
            $("#pageNum").val(currentPage);
            ajaxPage(currentPage);
        }
    });

    // 获得list数据
    var ajaxPage = function(pageNum) {
        var username = $.trim($('#user_name').val());
        var usertype = $('#user_type').val();
        var userstate = $('#user_state').val();
        var pageNum = pageNum;

        if(username.length == 0) {
            username = 'all';
        }

        if( usertype == '全部') {
            usertype = 'all';
        } else if(usertype == '管理员'){
            usertype = '1';
        } else if(usertype == 'B1运营专员') {
            usertype = '2';
        } else if(usertype == 'B2运营专员') {
            usertype = '3';
        }

        if( userstate == '全部') {
            userstate = 'all';
        } else if(userstate == '已启用'){
            userstate = '1';
        } else if(userstate == '禁用') {
            userstate = '0';
        }

        $.ajax({
            type: "post",
            url: '/manage/user/list.do',
            data: {
                username : username,
                role : usertype,
                state : userstate,
                pageNum : pageNum
            },
            cache: false,
            async : true,
            dataType: "json",
            success: function (data)
            {
                var trs = [];
                var tds = [];
                var trDom;
                var userList = data.data.list;

                $("#table tr:not(:first)").remove();
                for(var i = 0; i < userList.length; i++) {
                    trDom = $('<tr></tr>');
                    $('<td>' + userList[i].username + '</td>').appendTo(trDom);
                    $('<td>' + userList[i].phoneNumber + '</td>').appendTo(trDom);
                    $('<td>' + userList[i].domainAccount + '</td>').appendTo(trDom);
                    $('<td>' + userList[i].role + '</td>').appendTo(trDom);
                    $('<td>' + userList[i].createTime + '</td>').appendTo(trDom);
                    $('<td>' + userList[i].state + '</td>').appendTo(trDom);
                    if(userList[i].state == 0) {
                        $('<td id=' + userList[i].id + '>' + '<button type="button" class="btn btn-success" onclick="changState(this);">启用</button>' +
                            '<button class="btn btn-info" data-toggle="modal" data-target="#myModal" onclick="selectUser(this);">编辑</button>').appendTo(trDom);
                    } else {
                        $('<td id=' + userList[i].id + '>' + '<button type="button" class="btn btn-warning" onclick="changState(this)">禁用</button>' +
                        '<button class="btn btn-info" data-toggle="modal" data-target="#myModal" onclick="selectUser(this);">编辑</button>').appendTo(trDom);
                    }
                    trs.push(trDom);
                }
                for(var j = 0; j < trs.length; j++) {
                    $(trs[j]).appendTo($('#table'));
                }
                paging(data.data.pageNum, data.data.pages);
            },
            error:function () {
                alert("请求失败！");
            }
        });
    }
});